package com.sky.directdeal.fragment;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.sky.directdeal.R;
import com.sky.directdeal.activity.BusinessDetailsActivity;
import com.sky.directdeal.adapter.BusinessListAdapter;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.crouton.Style;
import com.sky.directdeal.utils.Utils;
import com.sky.local_ads.store_list.StoreDetails;

public class BusinessListFragment extends Fragment {
	private BusinessListAdapter storeAdapter;
	private ListView storeListView;
	private List<StoreDetails> businessDetailsList;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_business_list, container, false);
		storeListView=(ListView)rootView.findViewById(R.id.storeList);
		storeListView.setOnItemClickListener(listClickListener);
		
		return rootView;
	}
	
	public void setStoreList(List<StoreDetails> storeList) {
		this.businessDetailsList = storeList;
	}
	
	public void updateStoreList(){
		storeAdapter=new BusinessListAdapter(getActivity(),businessDetailsList);
		storeListView.setAdapter(storeAdapter);
		storeAdapter.notifyDataSetChanged();
		Log.e(" list frag", " on update list");
		
	}
	
	private OnItemClickListener listClickListener=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			StoreDetails storeDetails=businessDetailsList.get(pos);
			
			if(storeDetails!=null){
				Intent storeOfferIntent=new Intent(getActivity(),BusinessDetailsActivity.class);
				storeOfferIntent.putExtra(Constants.BUSINESS_DETAILS_INTENT, storeDetails);
				startActivity(storeOfferIntent);
			}else{
				Utils.displayToast(getActivity(), getString(R.string.no_details_found), Style.INFO);
			}
			
			
		}
	};
	
}
